GH-51019: [Python] Raise IndexError for out-of-bounds KeyValueMetadata key/value - #51020
Open
waterWang wants to merge 1 commit into
Open
GH-51019: [Python] Raise IndexError for out-of-bounds KeyValueMetadata key/value#51020waterWang wants to merge 1 commit into
waterWang wants to merge 1 commit into
Conversation
…etadata key/value KeyValueMetadata.key(i)/value(i) forwarded the index straight to the C++ KeyValueMetadata::key/value, whose bounds DCHECKs are compiled out in release builds. An out-of-range or negative index (e.g. on an empty metadata object, as reported) therefore performed an unchecked std::vector::operator[] access and segfaulted the process instead of raising a Python exception. Add an explicit bounds check in the Python bindings so negative and out-of-range indexes raise IndexError, matching the behavior of other indexed pyarrow containers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
KeyValueMetadata.key()segfaults on an out-of-range index #51019Rationale for this change
KeyValueMetadata.key(i)/.value(i)pass the index through to the C++KeyValueMetadata::key/valueaccessors, whoseDCHECKbounds checks arecompiled out in release builds. Any out-of-range or negative index — most
easily hit with
pa.KeyValueMetadata().key(0)on an empty object — performsan unchecked
std::vector::operator[]access and segfaults the Pythonprocess instead of raising an exception. Reproduced locally on pyarrow
25.0.1.
What changes are included in this PR?
Bounds checks in the Python bindings (
types.pxi) raiseIndexErrorfornegative or out-of-range indexes, matching the behavior of other indexed
pyarrow containers.
Are these changes tested?
Yes — added
test_key_value_metadata_index_errorscovering in-boundsaccess, out-of-range/negative indexes, and empty-metadata access. The
segfault reproduces on pyarrow 25.0.1 (SIGSEGV); with the fix the same
calls raise
IndexError.KeyValueMetadata.key()segfaults on an out-of-range index #51019KeyValueMetadata.key()segfaults on an out-of-range index #51019